package object3D;

import context.*;

import util.Util;
import geometry3D.Point3D;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import java.awt.geom.GeneralPath;



// ***************************************************************************
// *** CLASS: Etage2Trapp
// ***************************************************************************
public class InnvendigTrapp extends Block implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  
//  private boolean renderKjeller=false;
  private boolean render1Etage=false;
  private boolean render2Etage=false;
  
  public InnvendigTrapp(String ident,Point3D p0,double width,double height,double depth)
  { super(ident,p0,width,height,depth);
  }
  
  public void setRenderKjeller()
  { //renderKjeller=true;
    render1Etage=false;
    render2Etage=false;
  }
  
  public void setRender1Etage()
  { //renderKjeller=false;
    render1Etage=true;
    render2Etage=false;
  }
  
  public void setRender2Etage()
  { //renderKjeller=false;
    render1Etage=false;
    render2Etage=true;
  }
    
  // ***************************************************************************
  // *** METHOD: getModel3D
  // ***************************************************************************
//  public /*abstract*/ Group getModel3D(Appearance appearance)
//  { Group tg=new Group();
//    tg.addChild(trinn1.getModel3D(appearance));
//    tg.addChild(trinn2.getModel3D(appearance));
//    tg.addChild(trinn3.getModel3D(appearance));
//    tg.addChild(trinn4.getModel3D(appearance));
//    tg.addChild(trinn5.getModel3D(appearance));
//    return(tg);
//  }  


  public void drawProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  {

    Rectangle2D.Double r=projection(direction,origo2D,scale);
    switch(direction)
    {
      case NORTH: tegnTrapp(direction,g,r,origo2D,scale,c); break;
      case SOUTH: tegnTrapp(direction,g,r,origo2D,scale,c); break;
      case EAST: tegnTrapp(direction,g,r,origo2D,scale,c); break;
      case WEST: tegnTrapp(direction,g,r,origo2D,scale,c); break;
//      case ABOVE: drawProjectionAbove(g,r,scale,c); break;
//      case BELOW: drawProjectionBelow(g,r,scale,c); break;
//      case VCUT: drawVerticalCut(g,r,scale,c); break;
      case HCUT: tegnTrappPlan(g,r,origo2D,scale,c); break;
      default: super.drawProjection(direction,g,origo2D,scale,c);
    }  
  }

  private void tegnTrapp(int direction,Graphics2D g,Rectangle2D.Double r,Point2D.Double origo2D,double scale,Color c)
  {
  }

  private void tegnTrappPlan(Graphics2D g,Rectangle2D.Double r,Point2D.Double origo2D,double scale,Color c)
  {
//    Util.fill(g,r,Color.yellow);//c);
//    g.draw(r);    

    double xu1=r.x;
    double xu2=r.x+(1.0-0.45)*scale;
    double xu3=r.x+(1.0-0.45+0.50)*scale;
    double xm1=r.x+1.0*scale;
    double xm2=r.x+(1.0+0.05)*scale;
    double xm3=r.x+(1.0+0.0+0.20)*scale;
    double xm4=r.x+(1.0+0.0+0.40)*scale;
    double xm5=r.x+r.width;
    double xm6=xm5+(0.20)*scale;
    double xm7=xm6+(0.20)*scale;
    double xmE=xm7+(0.10)*scale;
    double ym1=r.y+1.0*scale;
    double ym2=ym1+0.05*scale;
    double ym3=r.y+r.height/2;
    double ym5=r.y+r.height-1.0*scale;
    double ym4=ym5-0.05*scale;
    double yu2=r.y+r.height;
    
    double yv3=ym3;
    double yv2=yv3-0.80*scale;
    double yv4=yv3+0.80*scale;
    
    // Tegn trapp del 1
    line(g,xm5,ym1,xm5,r.y);
    line(g,xm4,ym1,xm4,r.y);
    line(g,xm3,ym1,xu3,r.y);
    line(g,xm2,ym1,xu2,r.y);

    // Tegn trapp del 2 (midten)
    line(g,xu1,yv2,xm1,ym2);
    line(g,xu1,yv3,xm1,ym3);
    line(g,xu1,yv4,xm1,ym4);

    // Tegn trapp del 3
    line(g,xm5,ym5,xm5,yu2);
    line(g,xm4,ym5,xm4,yu2);
    line(g,xm3,ym5,xu3,yu2);
    line(g,xm2,ym5,xu2,yu2);

    if(render2Etage)
    { // Tegn trapp del 4 (extra 2. etg.)
      line(g,xm6,ym5,xm6,yu2);
      line(g,xm7,ym5,xm7,yu2);
    }
    // Tegn Vange
    double vangeBredde=0.05*scale;
    GeneralPath vange=new GeneralPath();
    vange.moveTo((float)xm5,(float)(ym1+vangeBredde));
    vange.lineTo((float)xm5,(float)(ym1));
    vange.lineTo((float)xm1,(float)(ym1));
    vange.lineTo((float)xm1,(float)(ym5));
    if(render2Etage)
    { vange.lineTo((float)xmE,(float)(ym5));
      vange.lineTo((float)xmE,(float)(ym5-vangeBredde));
    }
    else
    { vange.lineTo((float)xm5,(float)(ym5));
      vange.lineTo((float)xm5,(float)(ym5-vangeBredde));
    }
    vange.lineTo((float)(xm1+vangeBredde),(float)(ym5-vangeBredde));
    vange.lineTo((float)(xm1+vangeBredde),(float)(ym1+vangeBredde));
    vange.closePath();
    Util.fill(g,vange,Color.black);
    Util.draw(g,vange);
    
    // Tegn piler
    { double x0=r.x+0.70*scale;
      double y1=r.y+0.5*scale;
      double y2=r.y+1.8*scale;
      double xE=x0+0.80*scale;
      
      tegnPil(g,xE,y1,x0,y1);
      if(!render1Etage) tegnPil(g,x0,y2,xE,y2);
    }
  }
  
  private void tegnPil(Graphics2D g,double x1,double y1,double x2,double y2)
  { double dx=(x1-x2)*0.30; // 20% av lengden
    double dy=dx/2*1.5;    
    line(g,x1,y1,x2,y2);
    line(g,x2,y2,x2+dx,y2+dy);
    line(g,x2,y2,x2+dx,y2-dy); 
  }
  
  private void line(Graphics2D g,double x1,double y1,double x2,double y2)
  { g.draw(new Line2D.Double(x1,y1,x2,y2)); }

}
